package com.antd.common.auth.exception;

import com.antd.common.core.exception.BaseException;
import com.antd.common.core.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author Administrator
 */
@Slf4j
@RestControllerAdvice
public class RestCtrlExceptionHandler {

    @ExceptionHandler(BaseException.class)
    @ResponseStatus(value = HttpStatus.OK)
    public R<Object> handleAntdException(BaseException e) {
        String errorMsg = "Antd exception";
        if (e!=null){
            errorMsg = e.getMessage();
            log.error(e.toString(), e);
        }
        return R.error(errorMsg);
    }


    @ExceptionHandler(AccessDeniedException.class)
    @ResponseStatus(value = HttpStatus.OK)
    public R<Object> handleAccessDeniedExceptionException(AccessDeniedException e) {
        return R.error("抱歉，您没有访问权限，请联系管理员");
    }
}
